<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryObj.query"
            clearable
            @clear="getGoodsList"
            :disabled="false"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              class="search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!-- 添加用户区域 -->
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品展示区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column
          type="index"
          label="编号"
          width="49px"
        ></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="156px"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="156px"
        ></el-table-column>
        <el-table-column label="创建时间" width="188px">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{
              scope.row.add_time | dateForm
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="230px">
          <template slot-scope="scoped">
            <!-- 编辑按钮 -->
            <el-button
              icon="el-icon-edit"
              type="primary"
              size="mini"
              @click="editGoods(scoped.row.goods_id)"
              >编辑</el-button
            >
            <!-- 删除按钮 -->
            <el-button
              icon="el-icon-delete"
              type="danger"
              size="mini"
              @click="deleteGoods(scoped.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryObj.pagenum"
        :page-sizes="[15, 25, 35, 100]"
        :page-size="queryObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //请求商品列表的参数
      queryObj: {
        //搜索关键字
        query: '',
        //当前页面
        pagenum: 1,
        //显示数据的条数
        pagesize: 15
      },
      //商品列表
      goodsList: [],
      //总条数
      total: 0,
      //控制添加修改商品弹框的显示与隐藏
      addDialogVisible: false,
      //控制弹框的标题
      title: '添加商品',
      //添加或者修改表单
      goodsForm: {
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        goods_number: 100,
        goods_cat: '924'
      },
      //表单校验规则
      foodsFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          {
            min: 2,
            max: 80,
            message: '商品名称的长度为2~80个字符',
            trigger: 'blur'
          }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
          //   {min:2,max:80,message:'',trigger:'blur'}
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
          //   {min:2,max:80,message:'商品名称的长度为2~80个字符',trigger:'blur'}
        ]
      },
      //存储要修改商品的id
      goodsId: ''
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    //获取商品列表
    async getGoodsList() {
      const res = await this.$http.get('goods', { params: this.queryObj })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      console.log(this.goodsList)
      this.total = res.data.total
    },
    //监听pagesize变化
    handleSizeChange(newSize) {
      this.queryObj.pagesize = newSize
      this.getGoodsList()
    },
    //监听pasenum变化
    handleCurrentChange(newPage) {
      this.queryObj.pagenum = newPage
      this.getGoodsList()
    },
    //点击添加商品跳转到添加商品页面
    addGoods() {
      this.$router.push('/goods/add')
    },
    //点击删除按钮根据id删除对应的商品
    deleteGoods(gId) {
      this.$MessageBox
        .confirm('次操作将永久删除该用户,是否继续', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(async () => {
          const res = await this.$http.delete('goods/' + gId)
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          //删除成功
          this.$message.success(res.meta.msg)
          //重新获取商品列表
          this.getGoodsList()
        })
        .catch(() => this.$message.info('取消删除操作'))
    },
    //点击编辑按钮跳转到编辑页面
    editGoods(gId){
      this.$router.push('/goods/edit?id='+gId)
    }
  }
}
</script>

<style lang="less" scoped>
.search:hover {
  background-color: #409eff;
}
</style>
